package ldh.im.fxgui.demo;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import ldh.im.fxbase.component.LimitDataListView;

import java.util.ArrayList;
import java.util.List;

public class LimitDataListViewDemo extends Application {

    private Long id = 1L;

    @Override
    public void start(Stage stage) throws Exception {
        VBox vBox = new VBox();
        LimitDataListView<LimitData> limitDataListView = new LimitDataListView<LimitData>(data->{
            GridPane gridPane = new GridPane();
            gridPane.setHgap(10);
            gridPane.setVgap(20);
            TextArea textArea = new TextArea(data.getDesc()+"333333333333333333333333333");
            Label idLabel = new Label(data.getId()+"");
            idLabel.setMinWidth(30);
            gridPane.add(idLabel, 1, 1, 1, 2);
            gridPane.add(new Label(data.getName()), 2, 1);
            gridPane.add(textArea, 2, 2);
            int count = (int)(Math.random() * 10);
            gridPane.setOnMouseEntered(e->{
                gridPane.setBackground(new Background(new BackgroundFill(Color.BLUE, null, null)));
            });
            gridPane.setOnMouseExited(e->{
                gridPane.setBackground(null);
            });
            textArea.setPrefRowCount(count);

            return gridPane;
        });

        for(int i=0; i<10; i++) {
            List<LimitData> datas = new ArrayList();
            datas.add(randData());
            limitDataListView.addLasts(datas);
        }
        HBox hBox = new HBox(10);
        Button button = new Button("Add Front");
        button.setOnAction(e->{
            int t = (int)(Math.random() * 100);
            List<LimitData> datas = new ArrayList();
            for (int i=0; i<t; i++) {
                datas.add(randData());
            }
            System.out.println("add fronts:" + t);
            limitDataListView.addFronts(datas);

        });

        Button buttonLast = new Button("Add Last");
        buttonLast.setOnAction(e->{
            int t = (int)(Math.random() * 100);
            List<LimitData> datas = new ArrayList();
            for (int i=0; i<t; i++) {
                datas.add(randData());
            }
            System.out.println("add last:" + t);
            limitDataListView.addLasts(datas);
        });


        limitDataListView.setLoadLastCallback((Void)-> {
            buttonLast.fire();
            return null;
        });

        limitDataListView.setLoadFrontCallback((Void)-> {
            button.fire();
            return null;
        });
        hBox.getChildren().addAll(button, buttonLast);
        vBox.getChildren().addAll(limitDataListView, hBox);
        VBox.setVgrow(limitDataListView, Priority.ALWAYS);
        Scene scene = new Scene(vBox,400, 700);
        stage.setScene(scene);
        stage.show();
    }

    private LimitData randData() {
        LimitData limitData = new LimitData();
        limitData.setId(id++);
        limitData.setName(limitData.getId() + "");
        limitData.setDesc(limitData.getName());
        return limitData;
    }
}
